/**
 * Created by hucy on 2016-05-19.
 */
admin.controller('TermListCtrl',[
	'$scope',
  '$location',
	'$http',
  '$uibModal',
  'load2scope',
  'removeFromList',
  function($scope,$location,$http,$uibModal,load2scope ,removeFromList){
    load2scope( ['term' ,'major'] , $scope );
  	$scope.showDetail = function () {
  		var id = $('[name="ids"]:checked').val();
      if(!id) return;
  		$location.path('/detail/term/'+id);
  	};
    // 删除部门
    $scope.remove = function () {
      var option = {
        $scope : $scope,
        table  : 'term',
        modalConfig : {
          title:'删除确认',
          content:'您确定要删除该项？<br />\
                  <strong>此操作不可逆！</strong>',
        }
      };
      removeFromList(option);
    }

    $scope.setTerm = function ( termId ) {
      var oldTerm,newTerm;
      angular.forEach($scope.term, function(term, i){
        if ( term.term_status === 1 ) { 
          console.log( typeof term.term_status);
          console.log( term )
          term.term_status = 0;
          oldTerm = term;
        }
        if ( term.id === termId ) {
          term.term_status = 1;
          newTerm = term;
        }
      });
      var notice = show_stack('loading')
      $http.put('/api/term/'+oldTerm.id,oldTerm)
        .success(function (data) {
          console.log('success');
          console.log(data);

          $http.put('/api/term/'+newTerm.id,newTerm)
            .success(function (data2) {
              console.log('success2');
              console.log(data);
              notice.update( PNotify.prototype.myOpts['success-modify'] );
            })

            .error(function() {
              console.log('error2');
            });
        })

        .error(function() {
          console.log('error');
        });
    }
	}
])
.controller('TermDetailCtrl',[
	'$scope',
	'$uibModal',
  '$http',
	'$stateParams',
	'load2scope',
  'localStorageService',
  function($scope,$uibModal,$http,$stateParams,load2scope,localStorageService){
  	var id = $stateParams.id;

  	load2scope(['term'] , $scope);
    $scope.termItem = $scope.map.term[id];

    $scope.update  = true;
    $scope.tempterm = {};
    $scope.tempDate = parseInt($scope.termItem.term_date);
    $scope.tempTermDate = new Date( $scope.tempDate );
    console.log( typeof $scope.termItem.term_date);
  	// 切换修改状态
  	$scope.toggleUpdate = function () {

  		if( $scope.update ){
  			// 暂存
  			$.extend($scope.tempterm,$scope.termItem);
        $scope.tempDate = $scope.tempTermDate.getTime();
        console.log('暂存');
      }else{
        $.extend($scope.termItem,$scope.tempterm);
        $scope.tempTermDate = new Date( $scope.tempDate );
        console.log('取消');
      }
      $scope.update = !$scope.update;
    }
    // 修改班级信息
    $scope.modify = function () {
      $scope.termItem.term_date = $scope.tempTermDate.getTime();
      $http.put('/api/term/'+$scope.termItem.id , $scope.termItem).then(function (data) {
        // console.log(data);
        show_stack('success2');
        $scope.term[ $scope.term.indexOf($scope.tempterm) ] = $scope.termItem;
        localStorageService.set('term' , $scope.term);
        $scope.tempterm = $scope.termItem
        $scope.tempDate = parseInt($scope.termItem.term_date);
        $scope.toggleUpdate();

      }, function () {
        console.log('fail');
      });
    }
	}
])

.controller('TermAddCtrl', [
  '$scope',
  'localStorageService',
  '$http',
  '$location',

  'load2scope',
  function($scope,localStorageService,$http,$location,load2scope){

    load2scope(['term' ] ,$scope);

    $scope.termItem = {};
    var termList = $scope.term;
    var len = termList.length;
    var id  = parseInt(termList[len-1].id) + 1;
    console.log( id )
    $scope.termItem.id = id;

    $scope.add = function () {
      console.log($scope.termItem)
      termItem.term_date = $scope.tempTermDate.getTime();
      $http.post('/api/term', $scope.termItem)
        .success(function (data) {
          show_stack('success-add');
          termList.push($scope.termItem);
          localStorageService.set('term' , termList);
          $location.path('/detail/term/'+id);
        })
    }
    $scope.back = function () {
      $location.path('/list/term');
    }
  }
])

